#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@Time: 2018/1/10 下午5:17
@Author: CZC
@File: start_offline_parse_task.py
"""
import time
from collections import Iterable

import gevent
from gevent import monkey

from common import util
from common.logger import global_log
from config.mongodb_config import WebPageDB
from config.common_config import BaseConfig
from common.connectdb import MongoDB
from parser.parse_gsxt_worker import ParseGsxtWorker

monkey.patch_all()


class ParserTask(object):
    def __init__(self):

        self.worker_list = {}
        self.config_list = {}
        self.log = global_log
        self.mq_none_count = 0

        # 初始化worker
        self.init_worker()

        self.count = 0  # count任务数目

        # 连接网页库
        self.web_page_db = WebPageDB()
        self.db_connection = BaseConfig.mongodb_connection
        self.query = BaseConfig.source_select_param

    def init_worker(self):
        self.worker_list = ParseGsxtWorker()
        self.log.info('初始化worker完成..')

    def start_worker(self):

        start_time = time.time()

        try:
            self.task_run()
        except Exception as e:
            self.log.error('周期任务异常!!!!')
            self.log.exception(e)

        end_time = time.time()
        self.log.info('扫描起始时间: {st}'.format(st=start_time))
        self.log.info('扫描结束时间: {et}'.format(et=end_time))
        self.log.info('扫描消耗时间: {t}s'.format(t=end_time - start_time))

        self.log.info('完成扫描... 退出程序')

    def task_run(self):

        gevent.joinall([
            gevent.spawn(self.task, self.worker_list),
        ])

        current_time = 0
        sleep_time = 300
        self.log.info('完成解析!!, 开始休眠休眠时间为: {rand}'.format(rand=sleep_time))

        while current_time < 300:
            time.sleep(1)
            current_time += 1

    # 单个省份运行任务
    def task(self, work_list):
        self.log.info("任务开始……")
        data_list = self.__get_iterator_company_data()
        for company_data in data_list:
            if company_data:
                work_list.query_offline_task(company_data)
                self.log.info('当前任务: query = {query}'.format(query=self.query))
        else:
            self.log.info('当前查询语句已无结果返回{}'.format(self.query))

    def __get_iterator_company_data(self):
        web_page_db = MongoDB(self.web_page_db)
        data_list = web_page_db.find_data(self.db_connection, self.query)
        return data_list


def main():
    try:
        crawler = ParserTask()
        crawler.start_worker()
    except Exception as e:
        global_log.error("初始化错误:")
        global_log.exception(e)


if __name__ == "__main__":
    main()
